#include<iostream>
#include<string>
#include"App.h"

int main()
{

    auto app = uWS::App();

    /*
    //测试接口1
    app.post("/*", [](uWS::HttpResponse<false>* res, uWS::HttpRequest* req){
        res->end("recveved request!!");
        res->onData([](std::string_view chunk, bool isLast){
            if (isLast)
            {
                std::string str(chunk);
                std::cout << str << std::endl;
            }
        });
    });
    */
   app.ws<void*>("/*", {
        /* Settings */
        .compression = uWS::CompressOptions(uWS::DEDICATED_COMPRESSOR_4KB | uWS::DEDICATED_DECOMPRESSOR),
        .maxPayloadLength = 100 * 1024 * 1024,
        .idleTimeout = 16,
        .maxBackpressure = 100 * 1024 * 1024,
        .closeOnBackpressureLimit = false,
        .resetIdleTimeoutOnSend = false,
        .sendPingsAutomatically = true,
        /* Handlers */
        .upgrade = nullptr,
        .open = [](auto *ws) {
            /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
            //ws->send("test! test! test!", uWS::OpCode::BINARY, false);
            std::cout << "ws open" << std::endl;
        },
        .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
            /* This is the opposite of what you probably want; compress if message is LARGER than 16 kb
             * the reason we do the opposite here; compress if SMALLER than 16 kb is to allow for 
             * benchmarking of large message sending without compression */
            //ws->send(message, opCode, message.length() < 16 * 1024);

            /*
            uint32_t size = 0;
            char buffer[4096] = {};
            char* pos = buffer;

            //第一个测试的数据
            int a = -5;
            *((int*)pos) = a;
            pos += sizeof(int);
            size += sizeof(int);

            uint64_t b = 3555678;
            *((uint64_t*)pos) = b;
            pos += sizeof(uint64_t);
            size += sizeof(uint64_t);

            a = 7;
            *((int*)pos) = a;
            pos += sizeof(int);
            size += sizeof(int);

            const char* str = "abcdefg";
            memcpy(pos, str, strlen(str) + 1);
            size += strlen(str) + 1;

            ws->send(std::string_view(buffer, size), uWS::OpCode::BINARY, size < 16 * 1024);
            */

           std::cout << "ws message" << std::endl;

            char* buff = nullptr;
            FILE* pf = nullptr;
            pf = fopen("D:/RadarSSObjPosition.bin", "rb");
            fseek(pf, 0, SEEK_END);
            int fileLen = ftell(pf);
            fseek(pf, 0, SEEK_SET);
            buff = new char[fileLen];
            fread(buff, 1, fileLen, pf);
            fclose(pf);

            ws->send(std::string_view(buff, fileLen), uWS::OpCode::BINARY, fileLen < 16 * 1024);
        },
        .dropped = [](auto */*ws*/, std::string_view /*message*/, uWS::OpCode /*opCode*/) {
            /* A message was dropped due to set maxBackpressure and closeOnBackpressureLimit limit */
            std::cout << "ws dropped" << std::endl;
        },
        .drain = [](auto */*ws*/) {
            /* Check ws->getBufferedAmount() here */
            std::cout << "ws drain" << std::endl;
        },
        .ping = [](auto */*ws*/, std::string_view) {
            /* Not implemented yet */
        },
        .pong = [](auto */*ws*/, std::string_view) {
            /* Not implemented yet */
        },
        .close = [](auto */*ws*/, int /*code*/, std::string_view /*message*/) {
            /* You may access ws->getUserData() here */
            std::cout << "ws close" << std::endl;
        }
    });


    //监听
    app.listen("0.0.0.0", 3000, [](auto* listenSocket) {
	    if (listenSocket) {
			std::cout << "Listening on port " << 3000 << std::endl;
	    } else {
			std::cout << "Failed to listen on port 3000" << std::endl;
		}
	});

    //运行程序
    app.run();

    return 0;
}